// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: LZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 39 : TYPE_LZSETUP

// Function declarations
reserve function LZSetup_Function93
reserve function LZSetup_Function94
reserve function LZSetup_Function95
reserve function LZSetup_Function96
reserve function LZSetup_Function97
reserve function LZSetup_Function98
reserve function LZSetup_Function99
reserve function LZSetup_Function100
reserve function LZSetup_Function101
reserve function LZSetup_Function102

// Static Values
public value LZSetup_value30 = 0;
public value LZSetup_value29 = 0;

// Tables
public table LZSetup_table25
	2, 2, 1
end table

public table LZSetup_table1
	0xA000000, 0x62C0000, 8, 49, 0, 9, 8, 102, 0, 86, 2, 13, 0, 11, 2, 10, 0, 9, 2, 13, 0, 7, 8, 13, 56, 1, 40, 3, 8, 38, 0, 27, 1, 13, 0, 13, 2, 13, 0, 10, 2, 7, 8, 9, 56, 1, 40, 4, 8, 27, 0, 13, 2, 12, 0, 12, 2, 10, 0, 15, 2, 7, 0, 14, 2, 10, 0, 35, 48, 1, 32, 4, 0, 8, 2, 6, 
end table

function LZSetup_Function93
	stage[arrayPos0].deformationData1 = 1
	arrayPos0++
	stage[arrayPos0].deformationData1 = 1
	arrayPos0++
	stage[arrayPos0].deformationData1 = 2
	arrayPos0++
	stage[arrayPos0].deformationData1 = 2
	arrayPos0++
	stage[arrayPos0].deformationData1 = 3
	arrayPos0++
	stage[arrayPos0].deformationData1 = 3
	arrayPos0++
	stage[arrayPos0].deformationData1 = 3
	arrayPos0++
	stage[arrayPos0].deformationData1 = 3
	arrayPos0++
	stage[arrayPos0].deformationData1 = 2
	arrayPos0++
	stage[arrayPos0].deformationData1 = 2
	arrayPos0++
	stage[arrayPos0].deformationData1 = 1
	arrayPos0++
	stage[arrayPos0].deformationData1 = 1
	arrayPos0++
end function


function LZSetup_Function94
	stage[arrayPos0].deformationData1 = -1
	arrayPos0++
	stage[arrayPos0].deformationData1 = -1
	arrayPos0++
	stage[arrayPos0].deformationData1 = -2
	arrayPos0++
	stage[arrayPos0].deformationData1 = -2
	arrayPos0++
	stage[arrayPos0].deformationData1 = -3
	arrayPos0++
	stage[arrayPos0].deformationData1 = -3
	arrayPos0++
	stage[arrayPos0].deformationData1 = -3
	arrayPos0++
	stage[arrayPos0].deformationData1 = -3
	arrayPos0++
	stage[arrayPos0].deformationData1 = -2
	arrayPos0++
	stage[arrayPos0].deformationData1 = -2
	arrayPos0++
	stage[arrayPos0].deformationData1 = -1
	arrayPos0++
	stage[arrayPos0].deformationData1 = -1
	arrayPos0++
end function


function LZSetup_Function95
	object.value6 = temp1
	object.value6 >>= 16
	object.value7 = temp1
	object.value7 >>= 8
	object.value7 &= 255
	object.value8 = temp1
	object.value8 &= 255
	object.value9 = temp2
	object.value9 >>= 16
	object.value10 = temp2
	object.value10 >>= 8
	object.value10 &= 255
	object.value11 = temp2
	object.value11 &= 255
	if object.value9 == 0
		object.value9 = 1
	end if
	if object.value10 == 0
		object.value10 = 1
	end if
	if object.value11 == 0
		object.value11 = 1
	end if
	object.value6 <<= 8
	object.value7 <<= 8
	object.value8 <<= 8
	object.value6 /= object.value9
	object.value7 /= object.value10
	object.value8 /= object.value11
	if object.value6 == 0
		object.value6 = 256
	end if
	if object.value7 == 0
		object.value7 = 256
	end if
	if object.value8 == 0
		object.value8 = 256
	end if
end function


function LZSetup_Function96
	temp0 = 0
	GetPaletteEntry(1, 2, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 3, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 4, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 5, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	while temp0 < 64
		GetPaletteEntry(0, 2, temp1)
		GetPaletteEntry(1, 2, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 224
		temp1 /= object.value6
		temp2 *= 224
		temp2 /= object.value7
		temp3 *= 224
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(0, 3, temp1)
		GetPaletteEntry(1, 3, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 232
		temp1 /= object.value6
		temp2 *= 232
		temp2 /= object.value7
		temp3 *= 232
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(0, 4, temp1)
		GetPaletteEntry(1, 4, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 244
		temp1 /= object.value6
		temp2 *= 244
		temp2 /= object.value7
		temp3 *= 244
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(0, 5, temp1)
		GetPaletteEntry(1, 5, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
	loop
end function


function LZSetup_Function97
	temp0 = 0
	GetPaletteEntry(1, 13, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 17, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 18, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 16, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	while temp0 < 24
		GetPaletteEntry(0, 13, temp1)
		GetPaletteEntry(1, 13, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 248
		temp1 /= object.value6
		temp2 *= 248
		temp2 /= object.value7
		temp3 *= 248
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 17, temp1)
		GetPaletteEntry(1, 17, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 18, temp1)
		GetPaletteEntry(1, 18, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 16, temp1)
		GetPaletteEntry(1, 16, temp2)
		CallFunction(LZSetup_Function95)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
	loop
end function


function LZSetup_Function98
	temp0 = 0
	GetPaletteEntry(1, 26, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
	GetPaletteEntry(1, 27, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
	GetPaletteEntry(1, 28, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
end function


function LZSetup_Function99
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("Labyrinth_F.ogg", 0, 0x107D2, 0x1F40)
			break
		case 2
			SetMusicTrack("Labyrinth_F.ogg", 0, 0x107D2)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		case 4
		case 6
			SetMusicTrack("Labyrinth_F.ogg", 0, 0x107D2)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function LZSetup_Function100
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("Labyrinth.ogg", 0, 0x149DC, 0x30D4)
			break
		case 2
			SetMusicTrack("Labyrinth.ogg", 0, 0x149DC)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		case 4
		case 6
			SetMusicTrack("Labyrinth.ogg", 0, 0x149DC)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function LZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("ScrapBrain_F.ogg", 0, 1, 0x1F40)
			break
		case 2
			SetMusicTrack("ScrapBrain_F.ogg", 0, 1)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		case 4
		case 6
			SetMusicTrack("ScrapBrain_F.ogg", 0, 1)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function LZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("ScrapBrain.ogg", 0, 1, 0x30D4)
			break
		case 2
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		case 4
		case 6
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	if stage.activeList == BONUS_STAGE
		stage.waterState = 4
	endif
	object.value0++
	if object.value0 > 1
		tileLayer[0].deformationOffsetW++
		tileLayer[1].deformationOffsetW++
		object.value0 = 0
	end if
	object.value1++
	if object.value1 == 3
		object.value1 = 0
		RotatePalette(0, 171, 174, 0)
		RotatePalette(1, 171, 174, 0)
	end if
	if object.value2 > 0
		object.value2--
	else
		object.value3++
		object.value3 %= 3
		GetTableValue(object.value2, object.value3, LZSetup_table25)
		RotatePalette(0, 187, 189, LZSetup_value30)
		RotatePalette(1, 187, 189, LZSetup_value30)
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp1 = object[currentPlayer].xpos
		temp1 >>= 16
		temp2 = object[currentPlayer].ypos
		temp2 >>= 16
		temp2 += object[currentPlayer].collisionBottom
		temp2--
		Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_INDEX)
		CheckEqual(temp0, 71)
		temp3 = checkResult
		CheckEqual(temp0, 72)
		temp3 |= checkResult
		if temp3 == 1
			CheckEqual(object[currentPlayer].state, PlayerObject_KnuxGlideLeft)
			temp3 = checkResult
			CheckEqual(object[currentPlayer].state, PlayerObject_KnuxGlideRight)
			temp3 |= checkResult
			CheckEqual(object[currentPlayer].state, PlayerObject_KnuxWallClimb)
			temp3 |= checkResult
			CheckEqual(object[currentPlayer].state, PlayerObject_KnuxLedgePullUp)
			temp3 |= checkResult
			if temp3 == 1
				if object[currentPlayer].state == PlayerObject_KnuxGlideLeft
					FlipSign(object[currentPlayer].xvel)
					FlipSign(object[currentPlayer].speed)
				end if
				object[currentPlayer].state = PlayerObject_KnuxGlideDrop
				object[currentPlayer].animation = ANI_GLIDING_DROP
			end if
		end if
		if object[currentPlayer].gravity == GRAVITY_GROUND
			Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_ANGLEB)
			if temp0 == 1
				object[currentPlayer].state = PlayerObject_WaterSlide
				Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_DIRECTION)
				switch temp0
				case 0
				case 2
					object[currentPlayer].direction = FLIP_X
					break
				case 1
				case 3
					object[currentPlayer].direction = FLIP_NONE
				end switch
			end if
		end if
	next
	if object[0].state == PlayerObject_WaterSlide
		if object.value4 == 0
			if object.value5 == 0
				PlaySfx(SfxName[Waterfall], 0)
				StopSfx(SfxName[Waterfall Loop])
				object.value5 = 1
			else
				StopSfx(SfxName[Waterfall])
				PlaySfx(SfxName[Waterfall Loop], 0)
			end if
		end if
		object.value4++
		object.value4 &= 63
	else
		if object.value4 != 0
			object.value4++
			object.value4 &= 63
		else
			object.value4 = 0
			object.value5 = 0
		end if
	end if
	if LZSetup_value29 > 0
		LZSetup_value29--
	end if
end event


event ObjectDraw
	temp0 = stage.waterLevel
	temp0 -= screen.yoffset
	if temp0 < 0
		temp0 = 0
	end if
	if temp0 > screen.ysize
		temp0 = screen.ysize
	end if
	SetActivePalette(0, 0, temp0)
	if LZSetup_value29 > 0
		SetActivePalette(2, temp0, screen.ysize)
	else
		SetActivePalette(1, temp0, screen.ysize)
	end if
end event


event ObjectStartup
	if stage.actNum < 4
		SetMusicTrack("Labyrinth.ogg", 0, 0x149DC)
		SpeedUpMusic = LZSetup_Function99
		SlowDownMusic = LZSetup_Function100
	else
		SetMusicTrack("ScrapBrain.ogg", 0, 1)
		SpeedUpMusic = LZSetup_Function101
		SlowDownMusic = LZSetup_Function102
	end if
	animalType1 = TypeName[Pecky]
	animalType2 = TypeName[Rocky]
	object[10].type = TypeName[LZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 0
	GetTableValue(object[10].value2, 0, LZSetup_table25)
	arrayPos0 = 0
	CallFunction(LZSetup_Function93)
	arrayPos0 = 128
	CallFunction(LZSetup_Function93)
	arrayPos0 = 160
	CallFunction(LZSetup_Function94)
	arrayPos0 = 256
	while arrayPos0 < 576
		arrayPos1 = arrayPos0
		arrayPos1 -= 256
		stage[arrayPos0].deformationData1 = stage[arrayPos1].deformationData1
		arrayPos0++
	loop
	SetLayerDeformation(3, 128, 4, 0, 0, 0)
	if stage.actNum < 4
		LoadPalette("LZ_WaterPal.act", 1, 0, 0, 256)
	else
		LoadPalette("SBZ3_Pal.act", 0, 128, 128, 192)
		LoadPalette("SBZ3_WaterPal.act", 1, 0, 0, 256)
	end if
	CallFunction(LZSetup_Function96)
	CallFunction(LZSetup_Function97)
	CallFunction(LZSetup_Function98)
	LoadPalette("ElectricFlash.act", 2, 0, 0, 256)
	LZSetup_value29 = 0
	if options.attractMode == 1
		if credits.screen > 0
			PlayerObject_ReplayTable = LZSetup_table1
			PlayerObject_ReplaySize = 76
			PlayerObject_ReplayLength = 540
		end if
		CallFunction(PlayerObject_InitReplay)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
